//
//  YDOANoticeManVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/27.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import HandyJSON
class YDOANoticeManVC: UIViewController {
    @IBOutlet weak var myTableView: UITableView!
    
    lazy var dataSource : NSMutableArray = NSMutableArray()
    
    
    override func viewWillAppear(_ animated: Bool) {
         loadData();
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "公告管理"
        self.automaticallyAdjustsScrollViewInsets = false
        self.edgesForExtendedLayout = .init(rawValue: 0)
        setupUI()
       
    }
    
    
    func loadData() {
        let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject];
        let token = "token" //userinfo["token"] as! String
        let id = userinfo["id"] as? String
        let urlStr = String(format:GET_ANNOUN_CEMENTS,token,id! )
        YMLog.log(item: urlStr)
        YMNetworkManager().getRequest(urlString: urlStr, params:nil, success: { (responser) in
            YMLog.log(item: responser)
            let dataArr = responser["data"] as! NSArray
            for dict in dataArr{
                guard let model =  JSONDeserializer<YDOAAnnouncementModel>.deserializeFrom(dict: dict as? NSDictionary) else{return}
                self.dataSource.add(model)
            }
            self.myTableView.reloadData()
            
        }) { (error) in
            
        }
    }
    
    private func setupUI(){
        navigationController?.navigationBar.isHidden = false
        
        myTableView.register(UINib(nibName: "YDNoticeManCell", bundle: nil), forCellReuseIdentifier: "YDNoticeManCell")
        myTableView.separatorStyle = .none
        myTableView.reloadData()
    }
    
  
    
    //发布工作餐
    @IBAction func senderBtnClick(_ sender: Any) {
       
        navigationController?.pushViewController(YDOAAnnounceVC(), animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


extension YDOANoticeManVC : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 108
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            return 20
        }
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDNoticeManCell") as? YDNoticeManCell  else {
            return YDNoticeManCell()
        }
        
        cell.contentView.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
        guard let model = self.dataSource[indexPath.section] as? YDOAAnnouncementModel else{return cell;}
        cell.model = model
        weak var weakSelf = self
        cell.callBackWithId = {(id : Int) in
            guard let model =  self.dataSource[id] as? YDOAAnnouncementModel else{return  }
            let vc = YDOANoticeDetialVC()
            vc.model = model
            weakSelf?.navigationController?.pushViewController(vc, animated: true)
        }
        return cell

    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let model = self.dataSource[indexPath.section] as? YDOAAnnouncementModel else{return }
        let vc = YDOANoticeDetialVC()
        vc.model = model
        navigationController?.pushViewController(vc, animated: true)
    }
    

}
